<template>
  <div class="app-container">
    <!-- 搜索条件 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题名" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入标题名" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="活动日期" prop="activityTime">
        <el-date-picker clearable size="small" v-model="queryParams.activityTime" type="date" value-format="yyyy-MM-dd"
          placeholder="选择活动日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 页面操作事件 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['view:activity_information:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['view:activity_information:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['view:activity_information:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['view:activity_information:export']">导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!-- table表格 -->
    <el-table v-loading="loading" :data="activity_informationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align="center" />
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <el-table-column label="标题名" width="150" align="center" prop="title" />
      <el-table-column label="活动类型" align="center" prop="labelTitle">
        <template slot-scope="scope">
          {{ scope.row.labelTitle || "暂无" }}
        </template>
      </el-table-column>
      <el-table-column label="所属乡镇" align="center" prop="townShipName" />
      <el-table-column label="简介" align="center" prop="introduction" />
      <el-table-column label="联系电话" align="center" prop="phoneNumber">
        <template slot-scope="scope">
          {{ scope.row.phoneNumber || "暂无" }}
        </template>
      </el-table-column>
      <el-table-column label="图片" width="200" align="center" prop="image">
        <template slot-scope="scope">
          <el-image class="Image" v-for="(item, index) in scope.row.imagesUrl" :preview-src-list="scope.row.priveImg"
            :key="index" :src="baseurl + item" :lazy="true" alt style="width:50px,height:50px;"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="活动日期" align="center" prop="activityTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.activityTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报名总人数/报名人数" align="center">
        <template slot-scope="scope">
          {{ scope.row.peopleNumberLimit }}/{{ scope.row.peopleNumber }}
        </template>
      </el-table-column>
      <el-table-column label="签到/未签到人数" align="center">
        <template slot-scope="scope">
          {{ scope.row.comeNumber }}/{{ scope.row.noComeNumber }}
        </template>
      </el-table-column>
      <el-table-column label="是否上架" width="150" align="center" prop="flag">
        <template slot-scope="scope">
          <el-switch style="display: block" v-model="scope.row.flag" active-color="#13ce66" inactive-color="#ff4949"
            active-text="是" inactive-text="否" @change="putShelves(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="150" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-user" @click="handleUser(scope.row)"
            v-hasPermi="['view:activity_information:user']">报名人信息</el-button>
          <el-button size="mini" type="text" icon="el-icon-orange" @click="handleConfigure(scope.row)"
            v-hasPermi="['view:activity_information:configure']">活动配置</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['view:activity_information:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-tickets" @click="handleDetail(scope.row)"
            v-hasPermi="['view:activity_information:detail']">详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['view:activity_information:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改小程序活动信息对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="1000px" append-to-body
      @close="dioClose">
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="标题名" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题名" />
        </el-form-item>
        <el-form-item label="活动类型标题" prop="labelTitle">
          <el-input v-model="form.labelTitle" placeholder="请输入活动类型标题" />
        </el-form-item>
        <el-form-item label="活动图片" prop="image">
          <imageUpload v-model="form.image" @uploadImage="uploadimg" @remove="removeImg" :limit="6" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="所属乡镇" prop="townShipId">
          <el-select v-model="form.townShipId" placeholder="请选择" @change="townChange">
            <el-option v-for="item in townList" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="所属景区" prop="landscapesId">
          <el-select v-model="form.landscapesId" placeholder="请先选择所属乡镇">
            <el-option v-for="item in LandscapesList" :key="item.id" :label="item.landscapesName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属场所" prop="venueId">
          <el-select v-model="form.venueId" placeholder="请先选择所属乡镇">
            <el-option v-for="item in placeList" :key="item.id" :label="item.venueName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="地址" prop="address">
          <zxMap :value="latLng" :address="form.address" @input="mapClick" v-if="open"></zxMap>
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度" disabled />
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入纬度" disabled />
        </el-form-item>
        <el-form-item label="活动日期" prop="activityTime">
          <el-date-picker clearable size="small" v-model="form.activityTime" type="date" value-format="yyyy-MM-dd"
            placeholder="选择活动日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="时间段" prop="duration">
          <el-input v-model="form.duration" placeholder="请输入时间段" />
        </el-form-item>
        <el-form-item label="简介" prop="introduction">
          <el-input v-model="form.introduction" placeholder="请输入简介" />
        </el-form-item>
        <el-form-item label="详细介绍" prop="detailed">
          <editor v-if="open" v-model="form.detailed" :min-height="300" :height="300" />
        </el-form-item>
        <el-form-item label="活动截止报名日期" prop="endTime">
          <el-date-picker clearable size="small" v-model="form.endTime" type="date" value-format="yyyy-MM-dd"
            placeholder="选择活动日期">
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="活动群二维码" prop="qrCode">
          <imageUpload v-model="form.qrCode" @uploadImage="uploadimg" @remove="removeImg" :limit="6" />
        </el-form-item> -->
        <el-form-item label="注意事项" prop="process">
          <editor v-if="open" v-model="form.process" :min-height="300" :height="300" />
        </el-form-item>
        <el-form-item label="人数限制" prop="peopleNumberLimit">
          <el-input type="number" v-model="form.peopleNumberLimit" placeholder="请输入人数限制" />
        </el-form-item>
        <!-- <el-form-item label="限制条件" prop="warning">
          <el-input v-model="form.warning" placeholder="请输入限制条件" />
        </el-form-item> -->
        <el-form-item label="通知消息" prop="prohibit">
          <editor v-if="open" v-model="form.prohibit" :min-height="300" :height="300" />
        </el-form-item>
        <el-form-item label="是否上架" prop="flag">
          <el-radio v-model="form.flag" :label="true" :value="true">上架</el-radio>
          <el-radio v-model="form.flag" :label="false" :value="false">下架</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 详情 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="dlOpen" width="1000px" append-to-body
      class="xq-diolag">
      <p class="color:black;">标题名：{{ form.title }}</p>
      <p class="color:black;">活动类型标题：{{ form.labelTitle }}</p>
      <p class="color:black;">联系电话：{{ form.phoneNumber }}</p>
      <p class="color:black;">地址：{{ form.address }}</p>
      <!-- <p class="color:black;">图片：<el-image style="width: 100px; height: 100px;" v-for="(item,index) in form.priveImg" :key="index" :src="item"></el-image></p> -->
      <p class="color:black;">简介：{{ form.introduction }}</p>
      <p class="color:black;">活动日期：{{ form.activityTime }}</p>
      <p class="color:black;">活动时间段：{{ form.duration }}</p>
      <p class="color:black;">
        地址经纬度：{{ form.longitude + "," + form.latitude }}
      </p>
      <p class="color:black;">活动报名截至时间：{{ form.endTime }}</p>
      <p class="color:black;">活动流程：<span v-html="form.process"></span>
      </p>
      <p class="color:black;">限制条件：{{ form.warning }}</p>
      <p class="color:black;">禁止条件：<span v-html="form.prohibit"></span></p>
      <p class="color:black;">详细介绍：<span v-html="form.detailed"></span></p>
      <p class="color:black;">报名人数：{{ form.peopleNumber }}</p>
    </el-dialog>
  </div>
</template>

<script>
import {
  listActivity_information,
  getActivity_information,
  delActivity_information,
  addActivity_information,
  updateActivity_information,
  exportActivity_information,
  town_list,
  getUser,
  getLandscapes_list,
  getPlace_list,
  updateActivity_info,
} from "@/api/view/activity_information";
import ImageUpload from "@/components/UploadImage";
import Editor from "@/components/Editor";
import zxMap from "@/components/map/index";
export default {
  name: "Activity_information",
  components: {
    ImageUpload,
    Editor,
    zxMap,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 小程序活动信息表格数据
      activity_informationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层添加和修改
      open: false,
      //是否显示弹窗详情信息
      dlOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        detailed: null,
        activityTime: null,
        phoneNumber: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [{ required: true, message: "请输入", trigger: "blur" }],
        labelTitle: [{ required: true, message: "请输入", trigger: "blur" }],
        image: [{ required: true, message: "请选择", trigger: "change" }],
        phoneNumber: [{ required: true, message: "请输入", trigger: "blur" }],
        address: [{ required: true, message: "请输入", trigger: "blur" }],
        activityTime: [{ required: true, message: "请选择", trigger: "blur" }],
        duration: [{ required: true, message: "请输入", trigger: "blur" }],
        introduction: [{ required: true, message: "请输入", trigger: "blur" }],
        detailed: [{ required: true, message: "请输入", trigger: "blur" }],
        warning: [{ required: true, message: "请输入", trigger: "blur" }],
        prohibit: [{ required: true, message: "请输入", trigger: "blur" }],
        townShipId: [{ required: true, message: "请选择", trigger: "change" }],
        longitude: [{ required: true, message: '请在上方地图获取经纬度', trigger: 'blur' }],
        latitude: [{ required: true, message: '请在上方地图获取经纬度', trigger: 'blur' }],
        endTime: [{ required: true, message: '请选择', trigger: 'change' }],
        process: [{ required: true, message: '请输入', trigger: 'blur' }],
        flag: [{ required: true, message: '请选择', trigger: 'change' }],
        peopleNumberLimit: [{ required: true, message: '请输入', trigger: 'blur' }],
      },
      //用户信息
      userInfo: JSON.parse(sessionStorage.getItem("userInfo")),
      //乡镇列表
      townList: [],
      //获取景区列表
      LandscapesList: [],
      //场所列表
      placeList: [],
      latLng: "",
    };
  },
  created() {
    this.getList();
    this.getTownList();
  },
  methods: {
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        imagesUrl: null,
        address: null,
        detailed: null,
        label: null,
        duration: null,
        activityTime: null,
        phoneNumber: null,
        introduction: null,
        phoneNumber: null,
        labelTitle: null,
        longitude: null,
        latitude: null,
        warning: null,
        prohibit: null,
        endTime: null,
        // qrCode: null,
        process: null,
        peopleNumberLimit: 0,
      };
      this.resetForm("form");
    },
    /* 获取乡镇列表 */
    async getTownList() {
      let res = await town_list();
      let { code, data } = res;
      if (code === 200) {
        this.townList = data;
      };
    },
    /* 获取景区列表 */
    async getScenic(id) {
      let res = await getLandscapes_list({ townShipId: id });
      let { code, data } = res;
      if (code === 200) {
        this.LandscapesList = data;
      };
    },
    /* 获取场所列表 */
    async getPlace(id) {
      let res = await getPlace_list({ townShipId: id });
      let { code, data } = res;
      console.log(res, '所属场地');
      if (code === 200) {
        this.placeList = data;
      };
    },
    /** 查询小程序活动信息列表 */
    getList() {
      this.loading = true;
      listActivity_information(this.queryParams).then((response) => {
        let { code, rows, total } = response;
        console.log(rows, 'rows');
        if (code === 200) {
          rows.forEach((item) => {
            let priveImg = []; //图片预览数组
            item.imagesUrl = item.imageScale.split(",");
            item.imagesUrl.forEach((val) => {
              priveImg.push(this.baseurl + val);
              item.priveImg = priveImg;
            });

          });
          this.activity_informationList = rows;
          this.total = total;
          this.loading = false;
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    //图片上传事件
    uploadimg(e) {
      this.form.image = e;
    },
    //删除图片事件
    removeImg(e) {
      this.form.image = e;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加小程序活动信息";
    },
    /* 报名人信息按钮操作 */
    async handleUser(row) {
      this.$router.push({ path: 'signUp', query: { id: row.id } });
    },
    /* 活动配置按钮操作 */
    handleConfigure(row) {
      this.$router.push({ path: 'configure', query: { id: row.id, townShipId: row.townShipId } });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.title = "修改小程序活动信息";
      const id = row.id || this.ids;
      updateActivity_info({ id: id }).then(res => {
        this.latLng = res.data.longitude + "," + res.data.latitude;
        this.getScenic(res.data.townShipId);
        this.getPlace(res.data.townShipId);
        this.form = res.data;
        this.open = true;
      });
    },
    /* 详情按钮操作 */
    handleDetail(row) {
      this.title = "详情信息";
      updateActivity_info({ id: row.id }).then(res => {
        this.form = res.data;
        this.dlOpen = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.userId = this.userInfo.userId;
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateActivity_information(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addActivity_information(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除活动标题为"' + row.title + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delActivity_information(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有小程序活动信息数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportActivity_information(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
    /* 地图事件 */
    mapClick(e) {
      let latlng = e.lnglat.split(",");
      this.form.longitude = latlng[0];
      this.form.latitude = latlng[1];
      this.form.address = e.address;
      console.log(e, "emap");
    },
    /* 所属乡镇下拉框change事件 */
    townChange(id) {
      this.getScenic(id);
      this.getPlace(id);
    },
    /* dio关闭时触发 */
    dioClose() {
      this.latLng = "";
    },
    /* 是否上架change事件 */
    putShelves(val) {
      updateActivity_information(val).then((res) => {
        this.msgSuccess(res.msg);
        this.getList();
      }).catch(err => {
        this.getList();
      });
    },

  },
};
</script>
<style lang="scss" scoped>
.Image {
  width: 50px;
  height: 50px;
  margin: 0 2px;
}
</style>